define(function (require, exports, module) {
    var PDFTextObject = require('./Reader_TextObject.js');
    var PDFTextPage = require('./Reader_TextPage.js');

    /**
     * PDF text manager
     * @param readerApp app instance
     * @constructor
     * @private
     */
    WebPDF.PDFData.Text.TextManager = function (readerApp) {
        //var _readerApp = readerApp,
            /**
             * Text page list.
             * @type {{Number : WebPDF.PDFData.Text.PDFTextPage }}
             * @private
             */
            _textPageList = {},
            /**
             *Ajax请求标记数字，格式为：页面索引   ->  是否正在请求(true/false)
             * @type {{ Number : Boolean}}
             * @private
             */
            _textPageRequestingList = {},
            _selfTextManager = this;

        /**
         * @description 通过ajax获取某页文本页面信息
         * @param {Number} pageIndex 页面索引（从0开始）
         */
        this.ajaxGetTextPage = function (pageIndex) {
            if (_textPageRequestingList[pageIndex]){
                return;
            }

            var requestData = {};
            requestData.formMode = readerApp.isFormMode();
            requestData.s3Key = readerApp.getFileID();
            _textPageRequestingList[pageIndex] = true;
            $.ajax({
                url: readerApp.getOptions().url + "text/" + pageIndex,
                dataType: 'json',
                data: requestData,
                contentType:"application/x-www-form-urlencoded",
                async: true,
                success: function (response) {
                    var error = response.error;
                    if (error != 0) {
                        _textPageRequestingList[pageIndex] = false;
                        return;
                    }
                    try {
                        var jsonTextPageData = $.parseJSON(response.TextPageData);
                        if (jsonTextPageData != null) {
                            var textPage = new PDFTextPage();
                            textPage.init(jsonTextPageData);
                            _textPageList[pageIndex] = textPage;
                        }
                    }
                    catch (ex) {
                        console.error("Parse page{" + pageIndex + "} text failed.");
                        var textPage = new PDFTextPage();
                        textPage.init("");
                        _textPageList[pageIndex] = textPage;
                        _textPageRequestingList[pageIndex] = false;
                        return false;
                    }
                },
                error: function () {
                    console.error("Get page{" + pageIndex + "} text from server failed.");
                    var textPage = new PDFTextPage();
                    textPage.init("");
                    _textPageList[pageIndex] = textPage;
                    _textPageRequestingList[pageIndex] = false;
                    return false;
                }
            });
        };

        /**
         * @description 获取文本页面对象
         * @param {Number} nPageIndex 页面索引（从0开始）
         * @return {WebPDF.PDFData.Text.PDFTextPage | { text:null, blocking: true}} 文本页面对象;
         */
        this.getTextPage = function (pageIndex) {
            var page = _textPageList[pageIndex];
            if (!page) {       
                _selfTextManager.ajaxGetTextPage(pageIndex);
                if (_textPageList[pageIndex])
                    return _textPageList[pageIndex];
                return {text: null, blocking: true};
            }
            return page;
        };
    };

    return WebPDF.PDFData.Text.TextManager;
});